import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

from common.Utils import BlogDriver
#测试博客登录页面

class BlogLogin:
    url = ""
    driver = ""
    def __init__(self):
        self.driver = BlogDriver.driver
        self.url = "http://8.137.19.140:9090/blog_login.html"
        self.driver.get(self.url)

    #成功登陆的测试用例
    def LoginSucTest(self):
        #先清空再填写(养成习惯)
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        #由于浏览器密码安全提示问题，可以给予3秒时间进行手动安全弹窗提示关闭
        time.sleep(3)

        #能够找到博客首页用户的昵称，说明登陆成功，否则登陆失败
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
        #BlogDriver.getScreeShot()

        #因为先测试异常登录，所以这里点击注销，使系统成为未登录状态，为后面的页面元素测试做准备
        # self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(6)").click()

    #异常登录的测试用例
    def LoginFailTest(self):
        #先清空再填写
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        #正确的账号，错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        #检查是否出现弹窗并点击确认
        wait = WebDriverWait(self.driver,2)
        wait.until(EC.alert_is_present())
        alert = self.driver.switch_to.alert

        alert.accept()

        #先清空再填写
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        #错误的账号，正确的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        #检查是否出现弹窗并点击确认
        wait = WebDriverWait(self.driver,2)
        wait.until(EC.alert_is_present())
        alert = self.driver.switch_to.alert

        alert.accept()

        #先清空再填写
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        #错误的账号，错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()


        #检查是否出现弹窗并点击确认
        wait = WebDriverWait(self.driver,2)
        wait.until(EC.alert_is_present())
        alert = self.driver.switch_to.alert

        alert.accept()

    #界面元素测试用例
    def InterfaceElement(self):
        # 画面会闪现一下，添加隐式等待
        self.driver.implicitly_wait(3)

        #主页
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(4)").click()

        self.driver.find_element(By.CSS_SELECTOR, "body > div.container-login > div > h3")

        #写博客
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(5)").click()
        self.driver.find_element(By.CSS_SELECTOR, "#title").send_keys("hhh")
        self.driver.find_element(By.CSS_SELECTOR, "#editor > div.editormd-toolbar > div > ul > li:nth-child(21) > a").click()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        self.driver.find_element(By.CSS_SELECTOR, "body > div.container-login > div > h3")

# login = BlogLogin()
# login.LoginFailTest()
# login.InterfaceElement()
# login.LoginSucTest()